<?php
namespace app\cms\controller;
use app\common\base\_Db;
use app\common\entity\RequestEntity;
use think\Controller;
use think\Db;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/7
 * Time: 10:19
 */
class Stafftrade extends Controller{
    /*****************机场职工乘车订单*************************/
    /*
     * 数据查询
     */
    public function ListItem(){
        //默认为空列表，按时间查询
        $get_time = new RequestEntity('search_time', '搜索时间', false, 0);
        $get_did = new RequestEntity('search_did', '部门', false, 0);
        $param_page = input('Page', 1);
        $param_pagesize = 10;
        $where = "iden = 2 ";
        $start_time = mktime(0, 0, 0, date('m'), 1, date('Y'));
        $end_time = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
        $array_data_range = explode('|', $get_time->text);
        if(!empty($get_time->text||!empty($get_did->text))){
            if (is_array($array_data_range) && $array_data_range[0] !== '' && $array_data_range[1] !== '') {
                $where .= " and time between '" . $array_data_range[0] . " 00:00:00'" . " and '" . $array_data_range[1] . " 23:59:59'";
            }
            if (!empty($get_did->text)){
                $where .= ' and d.depart_id = ' . $get_did->text;
            }
            $result = Db::table('free_order')
                ->field(
                    [
                        'COUNT(a.uid)'	 =>'Count',
                        'b.nick_name'    =>'Name',
                        'd.depart_name'  =>'DepartName',
                    ]
                )
                ->alias('a')
                ->join('staff b','a.uid = b.staff_id','LEFT')
                ->join('department d','b.department_id = d.depart_id','LEFT')
                ->where($where)
                ->limit(($param_page - 1) * $param_pagesize, $param_pagesize)
                ->group('Name')
                ->order('d.depart_id desc')
                ->select();
            //echo Db::table('')->getLastSql();
            foreach ( $result as $key => $value){
                $result[$key]['Order'] = $key + 1;
            };
            //总条数
            $int_total_count = count(Db::table('free_order')
                ->field(
                    [
                        'COUNT(a.uid)'	 =>'Count',
                        'b.nick_name'    =>'Name',
                        'd.depart_name'  =>'DepartName',
                    ]
                )
                ->alias('a')
                ->join('staff b','a.uid = b.staff_id','LEFT')
                ->join('department d','b.department_id = d.depart_id','LEFT')
                ->where($where)
                ->group('Name')
                ->order('d.depart_id desc')
                ->select());
        }else{
            $result = '';
            $int_total_count = 0;
        }
        $data['tableData'] = $result;
        $data['TotalCount'] = $int_total_count;
        $data['Page'] = (int)$param_page;
        $data['PageSize'] = (int)$param_pagesize;
        _Db::ResponseJson($data);
    }
    /*
     * 详细信息
     * 每辆车乘车情况
     * ListItem->ItemInfo
     */
    public function ItemInfo(){
        //获取请求参数
        $request_data = [
            $post_name = new RequestEntity('Name', '姓名', true, 0),
            $post_departName = new RequestEntity('DepartName', '部门', true, 0),
            $post_time = new RequestEntity('search_time', '搜索时间', true, 0),
        ];
        _Db::Validates($request_data);
        $param_page = input('Page', 1);
        $param_pagesize = 10;
        $where = "iden = 2 ";
        $array_data_range = explode('|', $post_time->text);
        if (is_array($array_data_range) && $array_data_range[0] !== '' && $array_data_range[1] !== '') {
            $where .= " and time between '" . $array_data_range[0] . " 00:00:00'" . " and '" . $array_data_range[1] . " 23:59:59'";
        }
        if (!empty($post_name->text)){
            $where .= " and b.nick_name = '" . $post_name->text . " '" ;
        }
        if (!empty($post_departName->text)){
            $where .= " and d.depart_name = '" . $post_departName->text . " '" ;
        }
        $result = Db::table('free_order')
            ->field(
                [
                    'a.money'	     =>'Money',
                    'a.time'	     =>'Time',
                    'b.nick_name'    =>'Name',
                    'c.bus_number'	 =>'BusNumber',
                    /*'e.line_from'    =>'From',
                    'e.line_destination'   =>'Destination',*/
                ]
            )
            ->alias('a')
            ->join('staff b','a.uid = b.staff_id','LEFT')
            ->join('bus_info c','a.deviceid = c.vehicle_device_id','LEFT')
            ->join('department d','b.department_id = d.depart_id','LEFT')
            /*->join('line e','c.line_id = e.line_id','LEFT')*/
            ->where($where)
            ->limit(($param_page - 1) * $param_pagesize, $param_pagesize)
            ->order('d.depart_id desc')
            ->select();
        //echo Db::table('')->getLastSql();
        foreach ( $result as $key => $value){
            $result[$key]['Order'] = $key + 1;
            /*$result[$key]['Line'] = $value['From'].'--'.$value['Destination'];*/
        };
        //总条数
        $int_total_count = count(Db::table('free_order')
            ->field(
                [
                    'a.money'	     =>'Money',
                    'a.time'	     =>'Time',
                    'b.nick_name'    =>'Name',
                    'c.bus_number'	 =>'BusNumber',
                    /*'e.line_from'    =>'From',
                    'e.line_destination'   =>'Destination',*/
                ]
            )
            ->alias('a')
            ->join('staff b','a.uid = b.staff_id','LEFT')
            ->join('bus_info c','a.deviceid = c.vehicle_device_id','LEFT')
            ->join('department d','b.department_id = d.depart_id','LEFT')
            /*->join('line e','c.line_id = e.line_id','LEFT')*/
            ->where($where)
            ->order('d.depart_id desc')
            ->select());
        $data['tableData']=$result;
        $data['TotalCount'] = $int_total_count;
        $data['Page'] = (int)$param_page;
        $data['PageSize'] = (int)$param_pagesize;
        _Db::ResponseJson($data);
    }
}